<!--
 * @Description
 * @Author hanl
 * @Date 2024-07-19 16:21:39
 * @LastEditors hanl
 * @LastEditTime 2024-07-29 16:53:09
-->

<script setup lang="ts">
import type Typed from 'typed.js'

import { usePoetry, useTyped } from '@/hooks'

const poetryRef = ref<HTMLElement>()

useAnimate(poetryRef, [{ transform: 'scale(0)' }, { transform: 'scale(1)' }], 1000)

const poetryContentRef = ref<HTMLElement | null>(null)

const { origin, author, content, send } = await usePoetry()

const typedInstance = ref<Typed | null>(null)
const opacitySwitch = ref(false)
const showPoetryTitle = reactive({
  origin: origin.value,

  author: author.value
})

const { initTyped } = useTyped({
  instance: typedInstance as Ref<Typed>,
  contentRef: poetryContentRef,
  content: content.value,
  callback: () => {
    typedInstance.value?.stop()

    useParentElement(typedInstance.value!.cursor).value?.removeChild(typedInstance.value!.cursor)

    typedInstance.value?.destroy()

    typedInstance.value = null

    opacitySwitch.value = true

    useTimeoutFn(() => {
      opacitySwitch.value = false
      showPoetryTitle.author = author.value
      showPoetryTitle.origin = origin.value
    }, 200)

    nextTick(() => {
      initTyped()
    })
  },
  send
})

onMounted(() => {
  initTyped()
})
</script>

<template>
  <div
    ref="poetryRef"
    class="poetry relative h-150px min-w-500px flex-col-center origin-center select-none rounded-1 bg-origin-border backdrop-blur-sm"
  >
    <h2
      class="mb-10px text-24px font-bold transition duration-200 delay-0 ease-linear"
      :class="{ 'opacity-0': opacitySwitch }"
    >
      {{ showPoetryTitle.origin }}
    </h2>

    <h3
      class="mb-10px text-14px font-bold transition duration-200 delay-0 ease-linear"
      :class="{ 'opacity-0': opacitySwitch }"
    >
      {{ showPoetryTitle.author }}
    </h3>
    <div class="flex">
      <div
        ref="poetryContentRef"
        class="text-16px"
      />
    </div>
  </div>
</template>

<style lang="scss" scoped>
.poetry {
  box-shadow: 0 0 18px #ffffff80;

  &::after {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    content: '';
    background: #fff;
    filter: blur(5px);
    opacity: 0.3;
    z-index: -1;
  }
}
</style>
